//
//  SACollectionMessageDetaileViewController.m
//  SunOn
//
//  Created by mc on 2017/5/4.
//  Copyright © 2017年 NeoZhang. All rights reserved.
//

#import "SACollectionMessageDetaileViewController.h"
#import "SASalesCollectionTableViewCell.h"//销售人员cell
#import "SADepartmentCollectionTableViewCell.h"//部门人员cell

@interface SACollectionMessageDetaileViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tabview;
//发表评论所有视图父视图
@property (weak, nonatomic) IBOutlet UIView *allView;
//发表评论textfile
@property (weak, nonatomic) IBOutlet UITextField *commentTextFiled;
//发送按钮
@property (weak, nonatomic) IBOutlet UIButton *sendButton;
//layout
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *allViewBottomLayout;

@end

@implementation SACollectionMessageDetaileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view insertSubview:self.tabview belowSubview:self.allView];
    SAViewRadius(self.sendButton, 5);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillshow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHid:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - view cycle
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIButton *backbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backbutton.frame = CGRectMake(0, 0, 10, 19);
    [backbutton setBackgroundImage:SAImageNamed(@"ljiantou") forState:(UIControlStateNormal)];
    [backbutton addTarget:self action:@selector(handleBack) forControlEvents:(UIControlEventTouchUpInside)];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
    self.navigationItem.leftBarButtonItem = item;
}
#pragma mark - custom action
- (void)handleBack {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - view Action
- (IBAction)sendAction:(UIButton *)sender {
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SASalesCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SASalesCollectionTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell configureCellsImageHeight:150 * SAkHeight_scale CommentHeight:0];
        // x = 10  width = 357 * SAkWidth_scale  y = 89 + 9 + 150 * SAkHeight_scale height = 150 * SAkHeight_scale
        UIImageView *imagev = [[UIImageView alloc] initWithImage:SAImageNamed(@"组159副本3")];
        imagev.frame = CGRectMake(10, 89 + 9 + 150 * SAkHeight_scale, 357 * SAkWidth_scale, 150 * SAkHeight_scale);
        [cell.contentView addSubview:imagev];
        return cell;
    }
    SADepartmentCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SADepartmentCollectionTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CGFloat commentViewHeight = 38 + 20 * 4;
    [cell configureCellsImageHeight:0 CommentHeight:commentViewHeight];
    [cell writeCommentWithStr:@"任盈盈: 哈哈哈哈哈\n任盈盈: 呵呵呵\n任盈盈: 呵呵呵\n任盈盈: 呵呵呵\n任盈盈: 呵呵呵"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 89  + 9 + (150 * SAkHeight_scale) * 2;
    }
    return 111 + 44 + 11 + 20 * 4;
}
#pragma mark - NSNotificationCenter

- (void)keyboardWillshow:(NSNotification *)notification {
    if ([self.commentTextFiled isFirstResponder]) {
        //获取键盘弹起时间
        double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        //获取键盘上端Y坐标
        CGFloat keyboardy = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
        [UIView animateWithDuration:duration animations:^{
            self.allViewBottomLayout.constant = keyboardy;
        }];
    }
}

- (void)keyboardWillHid:(NSNotification *)notification {
    //获取键盘弹起时间
    double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.allViewBottomLayout.constant = 0;
    }];
}
#pragma mark - setter&getter
- (UITableView *)tabview {
    if (!_tabview) {
        self.tabview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SASCREEN_WIDTH, SASCREEN_HEIGHT - 50) style:(UITableViewStylePlain)];
        self.tabview.delegate = self;
        self.tabview.dataSource = self;
        [self.tabview registerNib:[UINib nibWithNibName:@"SASalesCollectionTableViewCell" bundle:nil] forCellReuseIdentifier:@"SASalesCollectionTableViewCell"];
        [self.tabview registerNib:[UINib nibWithNibName:@"SADepartmentCollectionTableViewCell" bundle:nil] forCellReuseIdentifier:@"SADepartmentCollectionTableViewCell"];
        self.tabview.showsVerticalScrollIndicator = NO;
        self.tabview.showsHorizontalScrollIndicator = NO;
        self.tabview.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tabview;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
